package com.ztools.pc.async;

import com.ztools.common.model.pc.store.OnlineStock;
import com.ztools.common.model.pc.store.StoreStock;
import com.ztools.common.model.wx.payment.Cart;
import com.ztools.common.model.wx.payment.CartItem;
import com.ztools.pc.service.store.OnlineStockService;
import com.ztools.pc.service.store.StoreStockService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2023/6/15  15:07
 * @Version 1.0
 */
@Async
@Component
public class StoreAsync {

    @Resource
    private OnlineStockService onlineStockService;
    @Resource
    private StoreStockService storeStockService;

    /**
     * 便利店消费扣除库存
     *
     * @param cart cart
     */
    public void storeSale(Cart cart) {
        List<CartItem> cartList = cart.getCartList();
        for (CartItem cartItem : cartList) {
            // 1.扣取线上商品上线数量
            OnlineStock onlineStock = onlineStockService.findBy("commodity_id", cartItem.getId()).get(0);
            onlineStock.setOnlineNumber(onlineStock.getOnlineNumber() - cartItem.getNums());
            onlineStockService.update(onlineStock);
            // 2.扣取商品库存
            StoreStock storeStock = storeStockService.findBy("commodity_id", cartItem.getId()).get(0);
            storeStock.setQuantity(storeStock.getQuantity() - cartItem.getNums());
            storeStockService.update(storeStock);
        }
    }

}
